package com.example.controller;


import com.example.dao.TrainingTypeCrudRepository;
import com.example.pojo.PageBean;
import com.example.pojo.TrainingType;
import com.example.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/trainingType")
@Slf4j
public class TrainingTypeController {
    @Autowired
    private TrainingTypeCrudRepository trainingTypeCrudRepository;
   

    @PostMapping("/addTrainingType")
    public Result add(@RequestBody TrainingType TrainingType){
        TrainingType save = trainingTypeCrudRepository.save(TrainingType);
        return Result.success(save);
    }
    @DeleteMapping("/deleteTrainingType")
    public Result delete(@RequestBody List<Integer> ids) {
        for (Integer id : ids) {
            trainingTypeCrudRepository.deleteById(id);
        }
        return Result.success("删除成功");
    }
    @PostMapping("/updateTrainingType")
    public Result update(@RequestBody TrainingType TrainingType){
        TrainingType save = trainingTypeCrudRepository.save(TrainingType);
        return Result.success(save);
    }
    @PostMapping("/getAllTrainingType")
    public Result getAllTrainingType(@RequestBody TrainingType TrainingType,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("TrainingType:{}",TrainingType);
        Pageable pageable = PageRequest.of(page - 1, pageSize, Sort.Direction.ASC, "id");

        Page<TrainingType> coursePage = trainingTypeCrudRepository.findAll(new Specification<TrainingType>() {
            @Override
            public Predicate toPredicate(Root<TrainingType> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //获取条件对象
                Predicate predicate = cb.conjunction();
                //判断用户对象
                if (TrainingType != null) {
                    // 根据 student 的属性添加查询条件
                    if (TrainingType.getName() != null) {
                        predicate.getExpressions().add(cb.like(root.get("name"), "%" + TrainingType.getName() + "%"));
                    }
                    if (TrainingType.getId() != null) {
                        predicate.getExpressions().add(cb.equal(root.get("id"), TrainingType.getId()));
                    }
                }
                return predicate;
            }
        }, pageable);
        List<TrainingType> TrainingTypes = coursePage.getContent();
        long elements = coursePage.getTotalElements();
        return Result.success(new PageBean(elements, TrainingTypes));
    }

    @GetMapping("/getTrainingTypeList")
    public Result getTrainingTypeList(){
        Iterable<TrainingType> all = trainingTypeCrudRepository.findAll();
        List<TrainingType> TrainingTypeList = new ArrayList<>();
        for (TrainingType TrainingType : all) {
            TrainingTypeList.add(TrainingType);
        }
        return Result.success(TrainingTypeList);
    }

}
